import { resolve } from 'path'
import { defineConfig, externalizeDepsPlugin, swcPlugin, loadEnv } from 'electron-vite'
import vue from '@vitejs/plugin-vue'
import proxy from './proxy'
import { useDateFormat } from '@vueuse/core'
import { loaderBanner } from './loader.banner'
import path from 'path'

// https://cn.electron-vite.org/guide/
// const env = loadEnv(mode, process.cwd())
export default defineConfig(({ command, mode }) => {
  const env = loadEnv(mode, process.cwd())
  // console.log('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>', command, mode)
  console.log(env, 'env')
  const buildTime = useDateFormat(new Date(), 'YYYY-MM-DD HH:mm:ss SSS')
  const isProd: boolean = command === 'build' && mode === 'production'
  const runtime = {
    env,
    isProd,
    buildTime: buildTime.value
  }
  return {
    main: {
      build: {
        rollupOptions: {
          input: {
            index: path.resolve(__dirname, 'src/main/index.ts')
          },
          output: {
            banner: loaderBanner
          }
        }
      },
      plugins: [swcPlugin(), externalizeDepsPlugin()]
    },
    preload: {
      plugins: [externalizeDepsPlugin()]
    },
    renderer: {
      // Deprecation [legacy-js-api]: The legacy JS API is deprecated and will be removed in Dart Sass 2.0.0.
      css: {
        preprocessorOptions: {
          scss: {
            api: 'modern-compiler'
          }
        }
      },
      resolve: {
        alias: {
          '@renderer': resolve('src/renderer/src')
        }
      },
      plugins: [vue()],
      preview: {
        proxy: proxy(env),
        cors: true
      },
      define: {
        Runtime: JSON.stringify(runtime)
      },
      server: {
        proxy: proxy(env),
        hmr: true
      }
    }
  }
})
